package com.capgemini.remind.controller;

import com.capgemini.core.AjaxResponse;
import com.capgemini.core.PageList;
import com.capgemini.remind.pojo.CustomerPromotion;
import com.capgemini.remind.pojo.HqPromotion;
import com.capgemini.remind.service.CommonsService;
import com.capgemini.remind.service.HqPromotionService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/promotion/hq")
public class HqPromotionController {
	// 日志记录器
	public final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private HqPromotionService hqPromotionService;
    @Autowired
    private CommonsService commonsService;

	@RequestMapping("/manage")
	public Object manage() {

		ModelAndView mv = new ModelAndView("/remind/promotionHQ.jsp");
        //保养项目
        List<String> serviceRemindItems = commonsService.queryByCodeValueGroupByType("ServiceRemindEnabled");
        mv.addObject("serviceRemindItems", serviceRemindItems);
		return mv;
	}

	@RequestMapping("/list")
	@ResponseBody
	public Object list(PageList<HqPromotion> pageList, HqPromotion pojo) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		pageList.setParam(pojo);
		hqPromotionService.queryPage(pageList);

		ajaxResponse.setData(pageList);

		return ajaxResponse;
	}
	@RequestMapping("/load/{id}")
	@ResponseBody
	public Object loadById(@PathVariable("id") String id) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		ajaxResponse.setData(hqPromotionService.getById(id));

		return ajaxResponse;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(HqPromotion pojo) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		if(pojo.getId() == null){
			hqPromotionService.insert(pojo);
		}else{
			hqPromotionService.updateById(pojo);
		}

		return ajaxResponse;
	}

	@RequestMapping("/selected")
	@ResponseBody
	public Object getSelectedByMaintenanceType(@RequestParam("maintenanceType") String maintenanceType){

		AjaxResponse ajaxResponse = new AjaxResponse();

		ajaxResponse.setData(hqPromotionService.getSelectedByMaintenanceType(maintenanceType));
//		ajaxResponse.setData(hqPromotionService.getSelected());

		return ajaxResponse;
	}


	@RequestMapping("/del/{id}")
	@ResponseBody
	public Object deleteById(@PathVariable("id") String id) {
		AjaxResponse ajaxResponse = new AjaxResponse();


		if(StringUtils.isBlank(id)){
			ajaxResponse.setSuccess(false);
			ajaxResponse.setErrorCode("PARAM-LOSS");
			ajaxResponse.setMessage("参数丢失");
		}else{
			hqPromotionService.deletePhysicalById(id);
		}

		return ajaxResponse;
	}

}
